package com.chang.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.common.to.MemberPrice;
import com.chang.common.to.SkuReductionTo;
import com.chang.common.utils.PageUtils;
import com.chang.common.utils.Query;
import com.chang.coupon.entity.MemberPriceEntity;
import com.chang.coupon.entity.SkuFullReductionEntity;
import com.chang.coupon.entity.SkuLadderEntity;
import com.chang.coupon.mapper.SkuFullReductionDao;
import com.chang.coupon.service.MemberPriceService;
import com.chang.coupon.service.SkuFullReductionService;
import com.chang.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {

        // 数据校验
        if (skuReductionTo.getFullCount() > 0 && skuReductionTo.getDiscount().compareTo(BigDecimal.ZERO) == 1) {
            // 1,保存sku的优惠满减等信息 sms_sku_ladder
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus()); // getCountStatus字段就是是否可以叠加其他优惠
            // todo 此处需要计算折后价格 skuLadderEntity.setDiscount();
            skuLadderService.save(skuLadderEntity);
        }


        // 保存满减信息
        if (skuReductionTo.getReducePrice().compareTo(BigDecimal.ZERO) == 1 && skuReductionTo.getFullPrice().compareTo(BigDecimal.ZERO) == 1) {
            SkuFullReductionEntity fullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo, fullReductionEntity);
            fullReductionEntity.setAddOther(skuReductionTo.getCountStatus());
            this.save(fullReductionEntity);
        }


        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && !memberPrice.isEmpty()) {
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().filter(item -> item.getPrice().compareTo(BigDecimal.ZERO) == 1).map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                if (item.getPrice() != null) {
                    memberPriceEntity.setMemberPrice(item.getPrice());
                }
                memberPriceEntity.setAddOther(1); // 默认可以叠加其他优惠
                return memberPriceEntity;
            }).toList();
            memberPriceService.saveBatch(memberPriceEntities);
        }


    }

}